package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.BxMsg;
import com.test.dao.BxMsgDao;
import com.test.entity.LeaveInfo;
import com.test.service.BxMsgService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报销信息(BxMsg)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("bxMsgService")
public class BxMsgServiceImpl implements BxMsgService {
    @Resource
    private BxMsgDao bxMsgDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BxMsg queryById(Integer id) {
        return this.bxMsgDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bxMsg 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public ResData queryByPage(BxMsg bxMsg, PageRequest pageRequest) {
        int total = Integer.parseInt(String.valueOf(this.bxMsgDao.count(bxMsg)));
        List<BxMsg> bxMsgList = bxMsgDao.queryAllByLimit(bxMsg, pageRequest);//查看一共有多少条数据
        int pageSize = pageRequest.getPageSize();//每页多少条
        int countPage = (total%pageSize==0)? (total/pageSize) : ((total/pageSize)+1);//一共有多少页
        List<MyObject> list = bxMsgList.stream().map(bxMsg1 -> new MyObject(bxMsg1)).collect(Collectors.toList());
        return new ResData(ResCode.SUCCESS,list,total,countPage);
    }

    /**
     * 新增数据
     *
     * @param bxMsg 实例对象
     * @return 实例对象
     */
    @Override
    public BxMsg insert(BxMsg bxMsg) {
        this.bxMsgDao.insert(bxMsg);
        return bxMsg;
    }

    /**
     * 修改数据
     *
     * @param bxMsg 实例对象
     * @return 实例对象
     */
    @Override
    public BxMsg update(BxMsg bxMsg) {
        this.bxMsgDao.update(bxMsg);
        return this.queryById(bxMsg.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bxMsgDao.deleteById(id) > 0;
    }
}
